package com.example.javasample.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.javasample.domain.TenantDO;
import com.example.javasample.mapper.TenantMapper;
import com.example.javasample.service.ITenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * TenantServiceImpl
 *
 * @author Kylin
 */
@Service
public class TenantServiceImpl implements ITenantService {

    @Autowired
    private TenantMapper tenantMapper;

    @Override
    public List<TenantDO> list(TenantDO tenant) {
//        QueryWrapper<TenantDO> queryWrapper = new QueryWrapper<>();
//        if (tenant != null) {
//            if (tenant.getId() != null) {
//                queryWrapper.eq("id", tenant.getId());
//            }
//            if (tenant.getName() != null) {
//                queryWrapper.like("name", tenant.getName());
//            }
//            if (tenant.getPhone() != null) {
//                queryWrapper.like("phone", tenant.getPhone());
//            }
//            if (tenant.getContact() != null) {
//                queryWrapper.eq("contact", tenant.getContact());
//            }
//        }
        // 这里更推荐用 LambdaQueryWrapper，可以避免硬编码数据库字段
        LambdaQueryWrapper<TenantDO> queryWrapper = new LambdaQueryWrapper<>();
        if (tenant != null) {
            if (tenant.getId() != null) {
                queryWrapper.eq(TenantDO::getId, tenant.getId());
            }
            if (tenant.getName() != null) {
                queryWrapper.like(TenantDO::getName, tenant.getName());
            }
            if (tenant.getPhone() != null) {
                queryWrapper.like(TenantDO::getPhone, tenant.getPhone());
            }
            if (tenant.getContact() != null) {
                queryWrapper.like(TenantDO::getContact, tenant.getContact());
            }
        }
        return tenantMapper.selectList(queryWrapper);
    }

    @Override
    public int add(TenantDO tenant) {
        return tenantMapper.insert(tenant);
    }

    @Override
    public int edit(TenantDO tenant) {
        return tenantMapper.updateById(tenant);
    }

    @Override
    public int delete(Long tenantId) {
        return tenantMapper.deleteById(tenantId);
    }

    /**
     * 物理删除
     *
     * @param tenantId 租户ID
     */
    @Override
    public int physicallyDelete(Long tenantId) {
        return tenantMapper.physicallyDelete(tenantId);
    }

}
